import pygame.sprite
#!/usr/bin/env python

try:
	import pdb
	import pygame
	import sys
	from data.scenarios import *
	from sprite.battlefield import *
	from sprite.infobar import *
	#~ from command import Command
	#~ from sprite.creature import Creature
	from sprite.cursor import *
	from sprite.panel import *
	from sprite.units import *
	from pygame.locals import *
	#~ from utils import *
except ImportError, err:
	print "Could not load %s module." % (err)
	raise SystemExit


class ScenarioEngine(object):
	def __init__(self):
		object.__init__(self)

		#~ self.clock = clock
		self.screen = pygame.display.get_surface()
		#~ self.scen_data = ScenarioDef()
		self.animating = False

		self.updaterects = None

		self.phase = 1

		# init layers (map, creatures, other, cursor)
		self.layers = pygame.sprite.LayeredDirty()

		self.field = Battlefield(scenarios[1].id, self.layers)
		self.info_bar = InfoBar(self.field, self.layers)
		self.units = Units(self.field, self.layers)

		self.layers.add(self.field)
		self.layers.add(self.info_bar, layer = 3)
		self.layers.add(self.units, layer = 2)

		cursor = Cursor(self, self.layers)

		self.inputstack = []
		self.inputstack.append(cursor)

		#sets cursor for the info bar and updates it
		self.info_bar.set_cursor(cursor)

		#Set a blank background for the layers
		background = pygame.Surface(pygame.display.get_surface().get_size())
		self.layers.clear(None, background)

	def draw_layers(self):
		self.__input_check()

		self.layers.update()
		self.updaterects = self.layers.draw(self.screen)


	def __input_check(self):
		if self.inputstack[-1].__class__ == Cursor:
			#Use key.get_pressed for continous movement - map/cursor scroll
			mods = pygame.key.get_mods()
			inputcheck = pygame.key.get_pressed()[273:277]
			if inputcheck[0] and not inputcheck[1]:
				self.do_cmove(K_UP, mods)
			if inputcheck[1] and not inputcheck[0]:
				self.do_cmove(K_DOWN, mods)
			if inputcheck[2] and not inputcheck[3]:
				self.do_cmove(K_RIGHT, mods)
			if inputcheck[3] and not inputcheck[2]:
				self.do_cmove(K_LEFT, mods)

	def do_cancel(self):
		self.inputstack[-1].close()

	def finish_window_close(self):
		"""
		Finishes closing the window. Called on USEREVENT - WINDOW_CLOSED
		"""
		self.inputstack.pop()

		self.inputstack[-1].show()

	def do_cmove(self, key, mod):
		self.inputstack[-1].c_move(key, mod) or \
			[spr.r_move(key, mod) for spr in self.layers.get_sprites_from_layer(0)]

	def do_move(self, key, mod):
		try:
			self.inputstack[-1].move(key, mod)
		except:
			pass


	def do_action(self):
		self.inputstack[-1].do_action(self)

	def load_sys_menu(self):
		self.inputstack.append(SystemMenu(self.layers, self))
		self.inputstack[-1].open()

	def load_creature_menu(self, creature):
		self.inputstack.append(CreatureMenu(self.layers, creature))
		self.inputstack[-1].open()

	def end_phase(self):
		self.phase += 1
		[unit.new_phase() for unit in self.layers.get_sprites_from_layer(1)]
		[infobar.update_phase(self.phase) for infobar in self.layers.get_sprites_from_layer(3)]


	def is_animating(self):
		if self.inputstack[-1].animating == [] or [[],[]]:
			return False
		return self.inputstack[-1].animating

	def bg_is_animating(self):
		return self.field.is_animating()
